<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" type="text/css" href="../bootstrap-table/dist/bootstrap-table.css"/>
 <script type="text/javascript" src="../bootstrap-table/dist/bootstrap-table.js"></script>
 <script type="text/javascript" src="../bootstrap-table/dist/locale/bootstrap-table-zh-CN.js"></script>

 <script type="text/javascript" src="../bootstrap-dialog/dist/js/bootstrap-dialog.js"></script>
 <link rel="stylesheet" type="text/css" href="../bootstrap-dialog/dist/css/bootstrap-dialog.css">
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
						<input type="hidden" id="pid" value="0">
						<ol class="breadcrumb" id="parentId">
							<li>
								<a href="javascript:selectObj(0)" onclick="removeLi(this)" >顶级分类列表</a>
							</li>
						</ol>
                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" onclick="selectParent()" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" onclick="deleteItemType()"  title="删除" ><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
								  <thead>
								  <tr>
									  <th class="" style="padding-right:0px">
										  <input type="checkbox" id="selall" class="icheckbox_square-blue">
									  </th>
									  <th class="sorting_asc">分类ID</th>
									  <th class="sorting">分类名称</th>
									  <th class="sorting">类型模板ID</th>

									  <th class="text-center">操作</th>
								  </tr>
								  </thead>
								  <tbody id="itemCatBody">
								  </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
              
  <div th:insert="/common/pageItem"></div>
  <!-- 编辑窗口 -->
  <div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" >
  	<div class="modal-content">
  		<div class="modal-header">
  			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
  			<h3 id="myModalLabel">商品分类编辑</h3>
  		</div>
  		<div class="modal-body">

  			<table class="table table-bordered table-striped"  width="800px">
  				<tr>
  		      		<td>上级商品分类</td>
  		      		<td name="parentName">

  		      		</td>
  		      	</tr>
  		      	<tr>
  		      		<td>商品分类名称</td>
  		      		<td><input  class="form-control" id="itemName" placeholder="商品分类名称">  </td>
  		      	</tr>
  		      	<tr>
  		      		<td>类型模板</td>
  		      		<td id="temSelect">

  		      		</td>
  		      	</tr>
  			 </table>

  		</div>
  		<div class="modal-footer">
  			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addItemCat()">保存</button>
  			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
  		</div>
  	  </div>
  	</div>
  </div>

  <!-- 编辑窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="updateModalLabel">商品分类编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>上级商品分类</td>
						  <input type="hidden" id="updateId">
						  <td name="parentName">

						  </td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>
						  <td><input  class="form-control" id="updateName" placeholder="商品分类名称">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td id="temUpdateSelect">

						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateItemType()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
</html>
<script>
	$(function (){
		selectObj(0);
	})

	function removeLi(obj){
		var liCount = $(obj).parents('li:first');
		if(liCount.prev().length <= 0){
			$(obj).parents('li:first').nextAll().remove();
		}else{
			$(obj).parents('li:first').prev().nextAll().remove();
		}

	}
	function selectObj(obj){
		$.ajax({
			url:"/tbItemCatController/selectItemCat",
			type:"post",
			data:{parentId:obj,pageNum:$("[name=pageNum]").val(),pageSize:$("[name=pageSize]").val()},
			dataType:"json",
			async:true,
			success:function (result){
				if(result.code == 100){
					var itemList = result.data.page.list;
					var table = ''
					for(var i = 0; i < itemList.length; i++){
						table += '<tr >'
							table += '<td><input name="boxs" value="'+itemList[i].id+'" type="checkbox" ></td>'
							table += '<td>'+itemList[i].id+'</td>'
							table += '<td>'+itemList[i].name+'</td>'
							table += '<td>'+itemList[i].typeId+' </td>'
							table += '<td class="text-center">'
								table += '<button type="button" class="btn bg-olive btn-xs" onclick="selectObj('+itemList[i].id+')" >查询下级</button>'
								table += '<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" onclick="updateItemCatReturn('+itemList[i].id+')" >修改</button>'
							table += '</td>'
						table += '</tr>'
					}
					$("#itemCatBody").html(table);
					buildPage(result.data.page);
					if(obj != 0 && $("#pid").val() != obj){
						var itemcat = result.data.itemcat;
						var liHtml = ''
						liHtml += '<li>'
						liHtml += '<a href="javascript:selectObj('+itemcat.id+')" onclick="removeLi(this)">'+itemcat.name+'</a>'
						liHtml += '</li>'
						$("#parentId").append(liHtml);
					}
					$("#pid").val(obj);
				}
				if(result.code == 10001){
					alert(result.message)
				}
			},
			error:function (){
				alert("查询网络异常");
			}
		})
	}

	function addItemCat(){
		$.ajax({
			url:"/tbItemCatController/addItemCat",
			type:"post",
			data:{parentId:$("#pid").val(),name:$("#itemName").val(),typeId:$("#itemType").val()},
			dataType:"json",
			async:true,
			success:function (result){
				if(result.code == 100){
					selectObj($("#pid").val());
				}
				if(result.code == 101){
					alert(result.message);
				}
			},
			error:function (){
				alert("增加异常");
			}
		})
	}

	function selectParent(){
		var parentText = $("#parentId").find("a");
		var nameText = "";
		for(var i = 0;i < parentText.length;i++){
			nameText += ">>" + $(parentText[i]).text();
		}
		$("[name=parentName]").text(nameText.substring(2));
		$.ajax({
			url:"/tbItemCatController/selectAll",
			dataType:"json",
			success:function (result){
				if(result.code == 100){
					var temSelect = result.data;
					var table = ''
					table += '<select id="itemType" style="width: 100%">'
					for(var i = 0; i < temSelect.length;i++){
						table += '<option value="'+temSelect[i].id+'">'+temSelect[i].name+'</option>'
					}
					table += '</select>'
					$("#temSelect").html(table);
				}

				if(result.code == 101){
					alert(result.message);
				}
			},
			error:function (){
				alert("查询网络异常");
			}
		})
	}
	$("#selall").click(function (){
		$("[name=boxs]").each(function (i,e){
			$(e).prop("checked",$("#selall").prop("checked"));
		})
	})

	function deleteItemType(){
		var ids = [];
		$("[name=boxs]:checked").each(function (i,e){
			ids.push($(e).val());
		})
		if(ids.length <= 0){
			alert("请选择要删除的数据");
			return ;
		}
		$.ajax({
			url:"/tbItemCatController/deleteItemType",
			type:"post",
			data:{ids:ids},
			dataType:"json",
			success:function (result){
				if(result.code == 100){
					alert(result.message);
					selectObj($("#pid").val());
				}
				if (result.code == 101){
					alert(result.message);
				}
			},
			error:function (){
				alert("修改网络异常");
			}
		})
	}

	//huix
	function updateItemCatReturn(obj){
		var parentText = $("#parentId").find("a");
		var nameText = "";
		for(var i = 0;i < parentText.length;i++){
			nameText += ">>" + $(parentText[i]).text();
		}
		$("[name=parentName]").text(nameText.substring(2));
		$.ajax({
			url:"/tbItemCatController/updateItemCatReturn",
			type:"post",
			data:{id:obj},
			dataType:"json",
			success:function (result){
				if(result.code == 100){
					var itemType = result.data.itemCat;
					$("#updateId").val(itemType.id);
					$("#updateName").val(itemType.name);
					$("#updateType").val(itemType.typeId);
					selectObj($("#pid").val());

					var temSelect = result.data.typeTem;
					var table = ''
					table += '<select id="updateType" style="width: 100%">'
					for(var i = 0; i < temSelect.length;i++){
						if(temSelect[i].id == itemType.typeId){
							table += '<option value="'+temSelect[i].id+'" selected>'+temSelect[i].name+'</option>'
						}else {
							table += '<option value="'+temSelect[i].id+'">'+temSelect[i].name+'</option>'
						}

					}
					table += '</select>'
					$("#temUpdateSelect").html(table);
				}
				if(result.code == 101){
					alert(result.message);
				}
			},
			error:function (){
				alert("修改网络异常");
			}
		})
	}

	function updateItemType(){
		$.ajax({
			url:"/tbItemCatController/updateItemType",
			type:"post",
			data:{id:$("#updateId").val(),name:$("#updateName").val(),typeId:$("#updateType").val()},
			dataType:"json",
			async:true,
			success:function (result){
				if(result.code == 100){
					selectObj($("#pid").val());
				}
				if(result.code == 101){
					alert(result.message);
				}
			},
			error:function (){
				alert("修改网络异常");
			}
		})
	}
</script>